<?PHP
/*
  Plugin for download from rapidshare (http://rapidshare.com/)
  Developed by seth <xd dot seth at gmail dot com - http://elrincondeseth.wordpress.com>
*/

$plugins[] = array('rapidshare','@http://(.*\.)?rapidshare.com/files/.+@');

/*
 This function returns a array:
    [status]       => one constant from include/defines.php
    [status_error] => the error message. Only if status == STATUS_ERROR
    [content]      => the file. Only if status != STATUS_ERROR
    [filePath]     => the filename. Optional
*/
function rapidshare_download($url, $extra)
{
    $file = downloadUrl($url);
    if (!preg_match('@<form id="ff" action="(.+?)" method="post">@', $file['content'], $matchs)){
        return array(
                    'status'       => STATUS_ERROR,
                    'status_error' => 'The file could not be found',
        );
    }elseif(($file['code']<200) or ($file['code']>299)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
        );
    }
    
    $file = downloadUrl($matchs[1], 'POST', 'dl.start=Free');
    if(!preg_match('@<form name="dlf" action="(.+?)" method="post">@', $file['content'], $matchs)){
        return array(
                    'status'       => STATUS_ERROR,
                    'status_error' => 'Error downloading the file',
        );
    }elseif(($file['code']<200) or ($file['code']>299)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
        );
    }
    
    $file = downloadUrl($matchs[1], 'POST', 'mirror=on');
    if(($file['code']<200) or ($file['code']>299)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
        );
    }elseif(strpos($file['content'], 'You have reached the download limit for free-users. Would you like more')!==FALSE){
        return array(
                    'status'       => STATUS_ERROR,
                    'status_error' => 'You have reached the download limit for free-users',
        );
    }else{
        return array(
                        'status'  => STATUS_DOWNLOADED,
                        'content' => $file['content']
        );
    }
    
}

/*
 This function returns the file name
*/
function rapidshare_getName($url)
{
    preg_match('@http://(.*\.)?rapidshare.com/files/[^/]+/(.*)@', $url, $matchs);
    return $matchs[2];
}
?>